package controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.UserDTO;

import DAO.BookDetailsDAO;
import DAO.DAOFactory;
import DAO.NotFoundException;
import DAO.UserDAO;

/**
 * Servlet implementation class MakeABookingSubmit
 */
@WebServlet("/MakeABookingSubmit")
public class MakeABookingSubmit extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MakeABookingSubmit() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProcess(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request, response);
	}
	
	protected void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String emergency=request.getSession().getAttribute("emergency").toString();
		String priority=request.getSession().getAttribute("priority").toString();
		if (priority.equals("High")){
			priority="1";
		}
		else if (priority.equals("Medium")){
			priority="2";
		}
		else{
			priority="3";
		}
		String fromDate=request.getSession().getAttribute("fromDate").toString();
		String toDate=request.getSession().getAttribute("toDate").toString();
		String reason=request.getSession().getAttribute("reason").toString();
		String facilityName=request.getSession().getAttribute("facilityName").toString();
		
		Date d = new Date(System.currentTimeMillis());
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String bookedOn = format.format(d);
		
		//Hardcode bookingID for demo the functions;
		int bookedBy = 10000003;
		String status = "Book";
		//Hardcode bookingID for demo the functions;
		int modifiedBy = 10000003;
		String modifiedOn = format.format(d);
		String comment = "";
		
		System.out.println("emergency:"+ emergency);
		System.out.println("priority:"+ priority);
		System.out.println("fromDate:"+ fromDate);
		System.out.println("toDate:"+ toDate);
		System.out.println("reason:"+ reason);
		System.out.println("facilityName:"+ facilityName);
		System.out.println("bookedOn:"+ bookedOn);
		System.out.println("bookedBy:"+ bookedBy);
		System.out.println("status:"+ status);
		System.out.println("modifiedBy:"+ modifiedBy);
		System.out.println("modifiedOn:"+ modifiedOn);
		System.out.println("comment:"+ comment);
		
		BookDetailsDAO bDAO= DAOFactory.loadInstance().getBookDetailsDAO();
		try {
			System.out.println("before insert");
			bDAO.insert(emergency, priority, fromDate, toDate, reason, facilityName, 
					bookedOn, bookedBy, status, modifiedBy, modifiedOn, comment);
			System.out.println("after insert");
		} catch (NotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		finally{
		//if the booking is emergent, send email to the user's manager to approve.
		if (emergency.equals("Y")){
			UserDAO uDAO=DAOFactory.loadInstance().getUserDAO();
			String toAddress="";
			try {
				toAddress=uDAO.findManagerEmail(bookedBy);
				System.out.println(toAddress);
			} catch (NotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String subject="A New Emergency Booking Waiting For Your Approval";
			String content="There is a new emergency booking waiting for your approval within " +
					"3 hours; Otherwise the system will *APPROVE* it automatically. Please DON'T " +
					"reply to this email as it's auto-generated";
			EmailController email=new EmailController();
			email.SendEmail(toAddress, subject, content);
		}
		
		//clean session.
		request.getSession().invalidate();
		}
		RequestDispatcher rd = null;
		rd=request.getRequestDispatcher("/MakeABooking.jsp");	
		rd.forward(request, response);
	}

}
